/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.lswms.controller;

import com.lswms.annotation.Login;
import com.lswms.annotation.LoginUser;
import com.lswms.common.page.PageInfo;
import com.lswms.common.page.PageRequest;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.dto.vo.req.CashOutReq;
import com.lswms.dto.vo.req.ChargeReq;
import com.lswms.dto.vo.req.QueryAmountReq;
import com.lswms.dto.vo.resp.QueryAmountResp;
import com.lswms.dto.vo.resp.TradeListResp;
import com.lswms.entity.YnUserEntity;
import com.lswms.service.YnPayRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.alibaba.fastjson.JSON;


/**
 * 交易相关接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/payrecord")
@Api(tags="交易相关接口")
@Slf4j
public class YnPayRecordController {
	
	@Autowired
	private YnPayRecordService ynPayRecordService;

    /**
     * 充值
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("charge")
    @ApiOperation("充值")
    @Login
    public Result<Object> charge(@RequestBody ChargeReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("充值，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	String resp = ynPayRecordService.charge(dto, ynuser);
    	log.info("充值，返回:{}",resp);
        return new Result<Object>().ok(resp);
    }
    /**
     * 充值回调
     * @param dto
     * @param request
     * @return
     */
    @GetMapping("notify")
    @ApiOperation("充值回调")
    public Result<Object> notify(HttpServletRequest request){
    	log.info("充值回调*********");
		String code = request.getParameter("code");
		String msg = request.getParameter("msg");
		String transactionId = request.getParameter("transactionId");
		if("00".equals(code) && "SUCCESS".equals(msg) && !StringUtils.isBlank(transactionId)){
	    	String resp = ynPayRecordService.notify("CHARGE",transactionId,request);
	    	log.info("充值回调，返回:{}",resp);
		}
		return new Result<Object>();
    }

    /**
     * 提现
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("cashOut")
    @ApiOperation("提现")
    @Login
    public Result<Object> cashOut(@RequestBody CashOutReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("提现，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	String resp = ynPayRecordService.cashOut(dto, ynuser);
        return new Result<Object>().ok(resp);
    }
    /**
     * 提现回调
     * @param dto
     * @param request
     * @return
     */
    @GetMapping("notifyOut")
    @ApiOperation("提现回调")
    public Result<Object> notifyOut(HttpServletRequest request){
    	log.info("提现回调*********");
		String code = request.getParameter("code");
		String msg = request.getParameter("msg");
		String transactionId = request.getParameter("transactionId");
		if("00".equals(code) && "SUCCESS".equals(msg) && !StringUtils.isBlank(transactionId)){
	    	String resp = ynPayRecordService.notifyCashOut("CASHOUT",transactionId,request);
	    	log.info("提现回调，返回:{}",resp);
		}
		return new Result<Object>();
    }

    /**
     * 根据年月查询收入支出
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("queryAmount")
    @ApiOperation("根据年月查询收入支出")
    @Login
    public Result<QueryAmountResp> queryAmount(@RequestBody QueryAmountReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("根据年月查询收入支出，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	QueryAmountResp resp = ynPayRecordService.queryAmount(dto, ynuser);
        return new Result<QueryAmountResp>().ok(resp);
    }
    
    /**
     * 账单记录
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("tradeList")
    @ApiOperation("账单记录")
    @Login
    public Result<PageInfo<TradeListResp>> tradeList(@RequestBody PageRequest dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
    	log.info("账单记录，参数：dto:{},user:{}",JSON.toJSONString(dto),JSON.toJSONString(ynuser));
    	ValidatorUtils.validateEntity(dto);
    	PageInfo<TradeListResp> resp = ynPayRecordService.tradeList(dto, ynuser);
        return new Result<PageInfo<TradeListResp>>().ok(resp);
    }


}